layui.use(['table', 'form', 'layer', 'laydate'], function () {
    var table = layui.table;
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var laydate = layui.laydate;

    table.render({
        elem: '#table'
        , url: '/emp/getEmp'
        , where: {
            department: 0,
            search: 0
        }
        , id: "emp-table"
        , page: true
        , height: 512
        , limit: 10
        , cols: [[
            {field: 'username', title: '用户名'}
            , {field: 'gender', title: '性别'}
            , {field: 'birthday', title: '出生年月', width: 180}
            , {field: 'idCard', title: '身份证', width: 180}
            , {field: 'area', title: '区域'}
            , {field: 'nativePlace', title: '籍贯'}
            , {field: 'state', title: '状态', sort: true}
            , {field: 'department', title: '部门'}
            , {field: 'level', title: '级别'}
            , {field: 'operation', title: '操作', fixed: 'right', width: 110, align: 'center', toolbar: '#barDemo'}
        ]]
    });

    var empForm = $('#newEmpForm');
    empForm.hide();
    laydate.render({
        elem: '#birthday'
    });

    var usernameInput = $('#username');


    table.on('tool(emp-table)', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        console.log(data);
        if (layEvent === 'edit') {

            form.val("newEmpForm", {
                "id": data.id,
                "username": data.username,
                "pwd": data.pwd,
                "gender": data.gender,
                "birthday": data.birthday,
                "idCard": data.idCard,
                "area": data.area,
                "nativePlace": data.nativePlace
                // "state":data.state,
                // "department":data.department,
                // "level":data.level
            });

            usernameInput.attr("disabled", true);


            layer.open({
                title: "编辑员工",
                type: 1,
                area: 'auto',
                maxWidth: 1000,
                content: $("#newEmpForm")
            });
            empForm.show();
        } else if (layEvent === 'access') {

            layer.open({
                title: "警告",
                content: '您没有此权限'
            });

        }
    });


    form.on('submit(editEmpSubmit)', function (data) {
        $.post('/record/editEmp', data.field, function (data) {
            if (!data.status) {
                layer.open({title: '警告', content: data.msg});
            } else {
                usernameInput.attr("disabled", false);
                empForm[0].reset();
                layer.closeAll();
                layer.open({
                    title: '成功',
                    content: "修改用户成功！"
                });
                table.reload('emp-table', {
                    where: {
                        department: 0,
                        search: 0
                    }
                })
            }
        });
        return false;
    });

    form.on('submit(search)', function (data) {
        table.reload('emp-table', {
            where: {
                department: 0,
                search: data.field.id
            }
        });

        return false;
    });

});